package fileOperator;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class DemoClass {
    public static void main(String[] args) throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        // 当前类对象
        FileOperatorMain fom = new FileOperatorMain();
        Class<FileOperatorMain> fom_reflect = FileOperatorMain.class;
        // 创建properties对象
        Properties ppt = new Properties();
        // 加载properties配置文件
        // 路径含有中文, 要用字符流读取
        ppt.load(new FileReader("FileOperator/resources/config.properties"));
        // 获取要执行的方法名
        String execute_method_name = ppt.getProperty("execute_method");
//        System.out.println(execute_method_name);


        // 根据方法名选择要执行的操作, 传递不同的参数
        switch (execute_method_name){
            case "testAllin":
                Method execute_method1 = fom_reflect.getDeclaredMethod(execute_method_name, FileOperatorMain.class,
                        String.class);

                // testAllin参数
                String allin_path = ppt.getProperty("allin_path");
                execute_method1.invoke(fom, fom, allin_path);
                break;
            case "testFileCopy":
                Method execute_method2 = fom_reflect.getDeclaredMethod(execute_method_name, FileOperatorMain.class,
                        String.class, String.class);

                // testFileCopy参数
                String target_path = ppt.getProperty("target_path");
                String start_path = ppt.getProperty("start_path");
                execute_method2.invoke(fom, fom, target_path, start_path);
                break;
            case "testFileCopyPlus":
                // 根据方法名获取方法对象
                Method execute_method3 = fom_reflect.getDeclaredMethod(execute_method_name, FileOperatorMain.class,
                        String.class, String.class, String[].class);

                // testFileCopyPlus参数
                String plus_target_path = ppt.getProperty("plus_target_path");
                String plus_start_path = ppt.getProperty("plus_start_path");
                String[] name = {""};
                        name[0] = ppt.getProperty("name");
                execute_method3.invoke(fom, fom, plus_target_path, plus_start_path, name);
                break;
            case "testFileCut":
                // 根据方法名获取方法对象
                Method execute_method4 = fom_reflect.getDeclaredMethod(execute_method_name, FileOperatorMain.class,
                        String.class, File.class);

                // testFileCut参数
                String cut_target_path = ppt.getProperty("cut_target_path");
                File file = new File("cut_file_path");
                execute_method4.invoke(fom, fom, cut_target_path, file);
                break;
        }
        System.out.println("程序执行完毕! ");
    }
}
